package org.infodavid.common.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import org.infodavid.common.IBuilder;
import org.infodavid.common.ejb.AccessControlListBean;
import org.infodavid.common.model.security.IAccessControlEntry;
import org.infodavid.common.model.security.IAccessControlList;
import org.infodavid.common.model.security.ISecuredObjectIdentifier;
import org.infodavid.common.model.security.ISecurityIdentifier;

/**
 * The Class AccessControlListTest.
 */
public class AccessControlListTest extends AbstractBeanTest<IAccessControlList> {

	/** The user test. */
	private final UserTest userTest = new UserTest(this);

	/**
	 * Instantiates a new test.
	 * @param mainTest the main test
	 */
	@SuppressWarnings("rawtypes")
	public AccessControlListTest(final AbstractBeanTest mainTest) {
		super(mainTest);
	}

	/**
	 * Instantiates a new test.
	 */
	public AccessControlListTest() {
		super();
	}

	/**
	 * Populate.
	 * @param count the count
	 * @return the collection< test data>
	 * @throws Exception the exception
	 */
	@Override
	protected List<IAccessControlList> doCreate(final int count) throws Exception {
		final IBuilder<IAccessControlList> builder =
		    ModelFactory.getInstance().getBuilder(IAccessControlList.class);
		final IBuilder<ISecurityIdentifier> identifierBuilder =
		    ModelFactory.getInstance().getBuilder(ISecurityIdentifier.class);
		final IBuilder<IAccessControlEntry> entryBuilder =
		    ModelFactory.getInstance().getBuilder(IAccessControlEntry.class);
		final IBuilder<ISecuredObjectIdentifier> objectIdentifierBuilder =
		    ModelFactory.getInstance().getBuilder(ISecuredObjectIdentifier.class);
		final Iterator<IUser> users = userTest.populate(count).iterator();
		final List<IAccessControlList> items = new ArrayList<IAccessControlList>(count);
		IAccessControlList item;
		IAccessControlEntry entry;
		IUser user;

		for (int i = 0; i < count; i++) {
			item = builder.create();
			user = users.next();
			entry = entryBuilder.create();

			item.setArchived(false);
			item.setName("item-" + System.nanoTime());
			item.setIdentifier(identifierBuilder.create());
			item.getIdentifier().setUser(user);
			item.getIdentifier().setGroup(user.getPrimaryGroup());

			entry.setObjectIdentifier(objectIdentifierBuilder.create());
			entry.getObjectIdentifier().setIdentifier(String.valueOf(RAND.nextInt()));
			entry.getObjectIdentifier().setType(IDocument.class.getName());

			items.add(item);
		}

		return items;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityClass()
	 */
	@Override
	public Class<AccessControlListBean> getEntityClass() {
		return AccessControlListBean.class;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityName()
	 */
	@Override
	public String getEntityName() {
		return IAccessControlList.class.getSimpleName();
	}
}
